import axios from 'axios'
import qs from 'qs'
// const baseURL = 'http://localhost:52112/'
const baseURL = 'http://wxtest.trfriend.com:12135/'
// const baseURL = 'http://wxapi.trfriend.com:9090/'
//const baseURL = ' http://3915b2ec.cpolar.io' //外网访问地址
// axios.defaults.baseURL = baseURL

// var instance = axios.create({ headers: 
//     {'content-type': 'application/x-www-form-urlencoded;charset=UTF-8','Access-Control-Allow-Credentials':'true',}  
//   });
axios.defaults.crossDomain = true;
axios.defaults.withCredentials = true;  //设置cross跨域 并设置访问权限 允许跨域携带cookie信息
var instance = axios.create({
  baseURL: baseURL,
  // baseURL:'http://192.168.2.199:12135/',
  timeout: 1000,
  headers: {
    'Content-Type':'application/x-www-form-urlencoded',
  }
});
  // axios.defaults.baseURL="http://wxtest.trfriend.com:12135/";//外网
  // axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

//请求拦截器,给所有的请求加上token
axios.interceptors.request.use(function (config) {
    //取出localStoasge中存储的token值
    let token = localStorage.getItem('mytoken')
    //设置请求头中Authorization这个名字是后台规定的
    config.headers['Authorization'] = token
    return config
}, function (error) {
    return Promise.reject(error)
})

//post请求
export const axiosPost = (url,params) => {
    //  return axios({
    //     method: 'post',
    //     url:url,
    //     data:params,
    //     transformRequest: [function (data) {
    //         // Do whatever you want to transform the data
    //         let ret = ''
    //         for (let it in data) {
    //           ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
    //         }
    //         return ret
    //       }],
    // }) 
     return instance.post(url,qs.stringify(params))
}

//get请求

export const axiosGet = (url,params) => {
   var paramStr="";
   for(let key in params)
   {
    paramStr+=key+"="+params[key]+"&";
   }
   paramStr=paramStr.substr(0,paramStr.length-1);
   if(paramStr){
     return instance.get(url+"?"+paramStr)
   }else{
    return instance.get(url);
   }
   
}

//下载pdf
export const axiosDownLoad=(url,params,contentType)=>{
  return instance.post(url,qs.stringify(params),{
    crossDomain:true,
    withCredentials:true,
    timeout: 60000,
    responseType:"blob",
    headers:{
      'Content-Type':'application/x-www-form-urlencoded',
    }
  })
}

export const requestUrl=baseURL;

/* //注册验证
export const register = (params,url) => {
  return instance.post('/Account/Register',qs.stringify(params))
}

//资产列表
export const assetList = (params,) =>{
  return instance.get('Asset/GetAssetTypeList',qs.stringify(parms))
} */

// 登录 
// export const login=(params)=>{
//     return axios.post("/Account/Login",params)
// }